package frame;

import pojo.UserDO;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");

    private JTextField transname = new JTextField();
    private JTextField transmoney = new JTextField();

    private UserService userService = new UserServiceImpl();

    public TransPanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel namelable = new JLabel("请输入转账人姓名：");
        namelable.setBounds(200, 200, 150, 30);
        namelable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(namelable);

        transname.setBounds(370, 200, 100, 30);
        transname.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transname);

        JLabel moneylable = new JLabel("请输入转账金额：");
        moneylable.setBounds(200, 250, 150, 30);
        moneylable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(moneylable);

        transmoney.setBounds(370, 250, 100, 30);
        transmoney.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transmoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 300, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 300, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            double balance = Double.parseDouble(transmoney.getText());
            try {
                userService.trans(atm.getLoginUser().getId(),transname.getText(),balance);
                JOptionPane.showMessageDialog(this,"转账成功");
                atm.switchPanel("menu");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception.getMessage());
            }

//            // 获取输入的对方账号
//            String otherName = transname.getText();
//            double balance = 0;
//            try {
//                // 获取输入的转账金额
//                balance = Double.parseDouble(transmoney.getText());
//            } catch (NumberFormatException ex) {
//                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
//                return;
//            }
//            // 当前登录的账户
//            Util db = new Util();
//            UserDO loginUser = db.getById(atm.getLoginUser().getId());
//            UserDO otherUser = db.getByUsername(otherName);
//
//
//            if (transname.getText().equals(loginUser.getUsername())) {
//                JOptionPane.showMessageDialog(this, "不能转账给自己");
//                return;
//            }
//            // 判断账号是否存在
//            if (otherUser == null) {
//                JOptionPane.showMessageDialog(this, "查无此人");
//                return;
//            }
//            // 判断余额是否足够
//            if (balance > loginUser.getMoney()) {
//                JOptionPane.showMessageDialog(this, "余额不足");
//                return;
//            }
//            // 当前账户扣钱
//            loginUser.setMoney(loginUser.getMoney() - balance);
//            // 对方账户加钱
//            otherUser.setMoney(otherUser.getMoney() + balance);
//
////            try {
////                db.getConnection();
////                db.setAutoCommit();
////                db.update(loginUser);
////                db.update(otherUser);
////                db.commit();
////                JOptionPane.showMessageDialog(this, "转账成功");
////            }catch (Exception e1){
////                db.rollback();
////                JOptionPane.showMessageDialog(this, "转账失败");
////            }finally {
////                db.close();
////            }
//            db.update(loginUser);
//            db.update(otherUser);
//            atm.switchPanel("menu");


//            for (int i = 0; i < atm.getUsers().size(); i++) {
//                if (transname.getText().equals(atm.getUsers().get(i).getUsername())) {
//                    if (balance > atm.getUsers().get(atm.getNum()).getMoney()) {
//                        JOptionPane.showMessageDialog(this, "钱不够，请重新输入");
//                        return;
//                    }
//                    JOptionPane.showMessageDialog(this, "转账金额为：" + transmoney.getText() + ",余额为" + (atm.getUsers().get(atm.getNum()).getMoney() - Double.parseDouble(transmoney.getText())));
//                    atm.getUsers().get(atm.getNum()).setMoney((atm.getUsers().get(atm.getNum()).getMoney()) - Double.parseDouble(transmoney.getText()));
//                    atm.getUsers().get(i).setMoney(atm.getUsers().get(i).getMoney() + Double.parseDouble(transmoney.getText()));
//                    String sql1="update t_user set u_money="+atm.getUsers().get(atm.getNum()).getMoney()+"where u_name='"+atm.getUsers().get(atm.getNum()).getUsername()+"'";
//                    Util.insert(sql1);
//                    String sql2="update t_user set u_money="+atm.getUsers().get(i).getMoney()+"where u_name='"+atm.getUsers().get(i).getUsername()+"'";
//                    Util.insert(sql2);


//            }

//            JOptionPane.showMessageDialog(this, "查无此人");

//        }
        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("login");
        }


    }
}

